<?php
global $m, $tab, $a;
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$df = $AppUI->getPref('SHDATEFORMAT');

$perm = &$AppUI->acl();
$hasRoleAccounting = $perm->userHasRole($AppUI->user_id, "Accounting");
if (!$hasRoleAccounting) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}

if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('AR_POST');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('AR_POST');
	}
	else {
		$_POST = $AppUI->getPOST('AR_POST');
	}
}

$page_offset = intval( dPgetParam( $_GET, 'page_offset', 0 ) );
$page_limit = intval( dPgetParam( $_POST, 'page_limit', 10));

//get post params
$customer = intval( dPgetParam( $_POST, 'project_company', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );
$from_date = dPgetParam( $_POST, 'complete_from_date', null );
if ($from_date != '') {
	$from_date = new CDate($from_date);
}
$to_date = dPgetParam( $_POST, 'complete_to_date', null );
if ($to_date != '') {
	$to_date = new CDate($to_date);
}

//0913.171.014
$canViewSalesAmount = $perm->checkModuleItem("taskfee", "view");
$canEditSalesAmount = $perm->checkModuleItem("taskfee", "edit");
$canDelete = $perm->checkModuleItem("projects", "delete");
// Load Object Status
$objMD = new CMdStatus();
//if ($hasRoleAccounting) {
//	$process_status = new CMDStatus();
//	$project_processed = dPgetSysVal( 'ProjectCompleted' );
//	$process_status->loadByCode($project_processed[0]);
//	$_POST['project_rstatus'] = $process_status->status_id;
//}
//showFVar($_POST);
$pro = new CProject();
$totalRows = 0;
$projects = $pro->getList($_POST, $totalRows);

require_once($AppUI->getMasterDataClass('mdcurrency'));
$objMD = new CMdCurrency();
$curArr = $objMD->getCurrencyList();

$objMD = new CCompany();
$cusList = $objMD->getAllowedRecords($AppUI->user_id, 'company_id, company_name', 'company_name');
$cusList = arrayMerge(array('0'=>"" . $AppUI->_(''). ""), $cusList);
?>
<form name="reportFrm" action="?m=<?=$m?>&tab=<?=$tab?>" method="post">
<input type="hidden" name="debit_invoice" value=""/>
<input type="hidden" name="debit_note" value=""/>
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="infopanel">
<tr><td width="50%">
<table cellspacing="0" cellpadding="4" border="0" width="100%" >
<tr>
	<td align="right" width="100px"><?=$AppUI->_('From date')?></td>
	<td nowrap="nowrap" width="100px">
		<input id="idDateFrom" type="hidden" name="complete_from_date"
				value="<?=$from_date ? $from_date->format( FMT_TIMESTAMP_DATE ) : ''?>" />
		<input id="idDateFromD" type="text" style="width: 80px"
				value="<?=$from_date ? $from_date->format( $df ) : ''?>"
				class="text" disabled="disabled" />
		<img id="idDateFromB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0">
	</td>
	<td align="right" width="80px"> <?=$AppUI->_('To date')?></td>
	<td nowrap="nowrap">
		<input id="idDateTo" type="hidden" name="complete_to_date"
				value="<?=$to_date ? $to_date->format( FMT_TIMESTAMP_DATE ) : ''?>" />
		<input id="idDateToD" type="text" style="width: 80px"
				value="<?=$to_date ? $to_date->format( $df ) : ''?>"
				class="text" disabled="disabled" />
		<img id="idDateToB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0">
	</td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Reference code')?></td>
	<td colspan="100%"><input type="text" name="keyword" value="<?=@$keyword?>" style="width: 316px"/></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Customer')?></td>
	<td colspan="100%"><?= arraySelect($cusList, 'project_company', ' id="customer" size="1" class="text" style="width: 320px;" onchange="doChangeCustomer(this.form, this.value)"', $customer, true)?></td>
</tr>
</table>
</td>
<td valign="top">
<table cellspacing="0" cellpadding="4" border="0" width="50%">
<?php
$curExchage = array();
foreach($curArr as $k => $cur) {
	$curExchage[$k] = dPgetParam( $_POST, $k . "_vnd", 1 );
	if ($cur == 'VND') {
		$_POST[$k . '_vnd'] = 1;
		continue;
	}
?>
	<tr>
		<td align="right"><?=$AppUI->_('Exchange rate: ')?></td>
		<td><input type="text" name="<?=$k?>_vnd" value="<?=dPgetParam( $_POST, $k . "_vnd", 16000 )?>" style="width: 80px; text-align: right"/></td>
	</tr>
<?php }?>
</table>
</tr></td>
</table>
<table cellspacing="0" cellpadding="6" border="0" width="100%" class="subinfopanel">
<tr>
	<td>
		<input class="button" type="button" name="searchAction" value="<?=$AppUI->_('Search');?>" onClick="doSearch(this.form)"/>
		<input class="button" type="button" name="exportAction" value="<?=$AppUI->_('Export To Excel');?>" onClick="doExport(this.form)"/>
		<input class="button" type="submit" name="resetAction" value="<?=$AppUI->_('reset');?>" onClick="return doReset(this.form)"/>
		<input class="button" type="button" name="printAction" value="<?=$AppUI->_('Print Debit Note');?>" onclick="showDebitNote(this.form)" <?=$customer > 0 ? '' : 'disabled="true"'?>/>
		<input class="button" type="button" name="handleFeeAction" value="<?=$AppUI->_('Details of Debit');?>" onclick="showHandleFee(this.form)" <?=$customer > 0 ? '' : 'disabled="true"'?>/>
	</td>
</tr>
</table>
</form>
<table id="idReferenceList" class="list" cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
			<th width="30px" rowspan="2" align="center"><?=$AppUI->_('#')?></th>
			<th rowspan="2"><?=$AppUI->_('Customer code')?></th>
			<th rowspan="2"><?=$AppUI->_('Customer name')?></th>
			<th rowspan="2"><?=$AppUI->_('Reference code')?></th>
			<th colspan="4" align="center"><?=$AppUI->_('Original currency')?></th>
			<th colspan="6" align="center"><?=$AppUI->_('VND')?></th>
		</tr>
		<tr>
			<th><?=$AppUI->_('Currency')?></th>
			<th><?=$AppUI->_('Sales amount')?></td>
			<th><?=$AppUI->_('VAT amount')?></th>
			<th><?=$AppUI->_('Account Receivable')?></th>
			<th><?=$AppUI->_('Inter')?></th>
			<th><?=$AppUI->_('On behalf')?></th>
			<th><?=$AppUI->_('Other')?></th>
			<th><?=$AppUI->_('Sales amount')?></td>
			<th><?=$AppUI->_('VAT amount')?></th>
			<th><?=$AppUI->_('Account Receivable')?></th>
	    </tr>
<?php if (is_array($projects) && count($projects) > 0):?>

	<script type="text/javascript">
		function submitForm(offset) {
			document.reportFrm.offset.value = offset;
			document.reportFrm.submit();
		}
	</script>
	<?php
		$i=0;
		$sumInter = 0;
		$sumOnBehalf = 0;
		$sumOther = 0;
		$sumSA = 0;
		$sumVAT = 0;
		$sumAR = 0;
		foreach ($projects as $row) {
			$i++;
			$currency = $row['project_currency'];
			$exchange = dPgetParam( $_POST, $currency . "_vnd", 1 );
			$inter = $pro->getExpenseAmount($row['project_id'], 1);
			$onbehalf = $pro->getExpenseAmount($row['project_id'], 2);
			$other = $pro->getExpenseAmount($row['project_id'], 3);
			$sa = $row['sa_sales_amount'];
			$vat = $row['sa_vat_amount'];
			$ar = $sa + $vat;

			$sumInter += $inter;
			$sumOnBehalf += $onbehalf;
			$sumOther += $other;
			$sumSA += $exchange * $sa;
			$sumVAT += $exchange * $vat;
			$sumAR += $exchange * $ar;
	?>
		<tr class="<?=($i % 2 != 0) ? "second" : "first" ?>" >
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=$i?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['company_code']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['company_name']?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" ><?=$row['project_iso_code']?></td>

			<!--------Original currency---------------->
			<td onClick="showReference(<?=$row['project_id']?>)" align="center"><?=$curArr[$row['project_currency']]?></td>
			<td align='right'><?=currencyFormat($row['sa_sales_amount'], $row['project_currency'])?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=currencyFormat($pro->getVATAmount($row['project_id']), $row['project_currency'])?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($pro->getAccountReceivable($row['project_id']))?></td>

			<!--------VND---------------->
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($inter)?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($onbehalf)?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($other)?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($exchange * $sa)?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($exchange * $vat)?></td>
			<td onClick="showReference(<?=$row['project_id']?>)" align="right"><?=number_format($exchange * $ar)?></td>
		</tr>
	<?php }?>
		<tr>
			<th colspan="8" align="right"><?=$AppUI->_('Total:')?></th>
			<th align="right"><?=number_format($sumInter)?></th>
			<th align="right"><?=number_format($sumOnBehalf)?></th>
			<th align="right"><?=number_format($sumOther)?></th>
			<th align="right"><?=number_format($sumSA)?></th>
			<th align="right"><?=number_format($sumVAT)?></th>
			<th align="right"><?=number_format($sumAR)?></th>
		</tr>
	</table>
<?php else:
	showEmptyRow('1', 'second', 'Not found any references', '100%');
endif;?>
</table>
<script>
	Calendar.enabled = true;

	Table("idReferenceList").setNormalHover(1);

	function showReference(id){
		location.href = "index.php?m=projects&a=view&project_id=" + id;
	}

	function doSearch(frm) {
		frm.action = '?m=<?=$m?>&a=<?=$a?>&suppressHeaders=0';
		frm.submit();
	}

	function doReset(frm) {
		frm.action = '?m=<?=$m?>&a=<?=$a?>&suppressHeaders=0';
		return true;
	}

	function doExport(frm) {
		frm.action = '?m=reports&a=do_r_account_receivable&suppressHeaders=1';
		frm.submit();
	}

	function doPrintDebitNote(frm) {
		var frm = document.reportFrm;
		frm.debit_invoice.value = $('debit_invoice').value;
		frm.debit_note.value = $('debit_note').value;
		frm.target = '_new';
		frm.action = '?m=projects&a=do_print_debit_note&suppressHeaders=1';
		frm.submit();
		frm.target = '';
		Dialog('idDebitNote').hide();
	}

	function showHandleFee(frm) {
		frm.action = '?m=projects&a=do_debit_details&suppressHeaders=1';
		frm.submit();
	}

	function showDebitNote(frm)
	{
		var cd = Control('idDebitNote').create('div', 'popup', 'hidden');
		Dialog(cd, 1).show();
	}

	function doChangeCustomer(frm, sel) {
		frm.printAction.disabled = (sel == 0);
		frm.handleFeeAction.disabled = (sel == 0);
	}
</script>
<div id="idDebitNote" class="popup" style="visibility: hidden; left: 436px; top: 94.5px; display: none;">
<?php include('debit_note.php');?>
</div>